<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\inter\logic;

use app\inter\model\OrderCommentModel;
use app\inter\model\OrderModel;
use app\inter\model\OrderCommonModel;
use app\api\model\CouponModel;
use think\Db;
use think\Model;

class OrderLogic extends Model
{
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * 获取订单信息
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $order
     * @param $page
     * @param $limit
     * @return array|false
     **/
    public function getOrderCommon($map=array(),$field='a.*',$flag=true,$order='a.id desc',$page=1,$limit=10,$is_real='')
    {
        if(!is_array($map)){
            return false;
        }
        $order_common_model = new OrderCommonModel();
        return $order_common_model->getOrderCommon($map,$field,$flag,$order,$page,$limit,$config='',$is_real);
    }

    /**
     * 根据id获取订单信息
     * @param $common_id
     * @param $field
     * @return array|false
     * */
    public function getOrderCommonById($common_id,$field = 'a.*')
    {
        if(empty($common_id)){
            return false;
        }
        $order_common = new OrderCommonModel();
        return $order_common->getOrderCommon(array('a.id'=>$common_id),$field,false);
    }

    /**
     * 根据id获取订单详情信息
     * @param $common_id
     * @param $field
     * @return array|false
     * */
    public function getOrderCommonDetailById($common_id,$field = 'a.*')
    {
        if(empty($common_id)){
            return false;
        }
        $order_common = new OrderCommonModel();
        return $order_common->getOrderCommonDetail(array('a.id'=>$common_id),$field,false);
    }

    /**
     * 生成订单
     * @param $data
     * @return int|false
     * */
    public function createOrderCommon($data)
    {
        if(empty($data)){
            return false;
        }
        $order_common = new OrderCommonModel();
        return $order_common->addOrderCommon($data);
    }

    /**
     * 更新订单
     * @param $map
     * @param $data
     * @return int|false
     * */
    public function renewOrderCommon($map=array(),$data)
    {
        if(empty($data)){
            return false;
        }
        $order_common = new OrderCommonModel();
        return $order_common->updateOrderCommon($map,$data);
    }

    /**
     * 获取订单乘客关系信息
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $order
     * @param $page
     * @param $limit
     * @return array|false
     * */
    public function getOrder($map=array(),$field = 'b.*',$flag = true,$order='b.id desc',$page=1,$limit=10)
    {
        $orderModel = new OrderModel();
        return $orderModel->getOrderInfo($map,$field,$flag,$order,$page,$limit);
    }

    /**
     * 根据id获取订单乘客关系信息
     * @param $order_id
     * @param $field
     * @return array|false
     * */
    public function getOrderById($order_id,$field = 'b.*')
    {
        if(empty($order_id)){
            return false;
        }
        $orderModel = new OrderModel();
        $map['a.id'] = $order_id;
        return $orderModel->getOrderInfo($map,$field,false);
    }

    /**
     * 根据common_id获取订单乘客关系数量
     * @param $common_id
     * @return int|false
     * */
    public function orderCountByCommonId($common_id)
    {
        $order_model = new OrderModel();
        $map['common_id'] = $common_id;
        return $order_model->orderCount($map);
    }

    /**
     * 创建订单乘客关系
     * @param $data
     * @param $flag //true添加多条，false添加一条
     * @return int|false|string
     * */
    public function createOrder($data,$flag = true)
    {
        if(empty($data)){
            return false;
        }
        $order_model = new OrderModel();
        return $flag ? $order_model->addOrderAll($data):$order_model->addOrder($data);
    }

    /**
     * 获取订单评价信息
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $order
     * @param $page
     * @param $limit
     * @return array|false
     **/
    public function getOrderComment($map=array(),$field='*',$flag=true,$order='comment_id desc',$page=1,$limit=10)
    {
        if(!is_array($map)){
            return false;
        }
        $order = $order?:'comment_id desc';
        $order_common_model = new OrderCommentModel();
        return $order_common_model->getOrderComment($map,$field,$flag,$order,$page,$limit);
    }

    /**
     * 生成订单评价
     * @param $data
     * @return int|false
     * */
    public function createOrderComment($data)
    {
        if(empty($data)){
            return false;
        }
        $order_model = new OrderCommentModel();
        return $order_model->addOrderComment($data);
    }

    /**
     * 获取订单评论的数量
     * @param $map
     * @return int|boolean
     * */
    public function orderCommentCount($map)
    {
        $order_model = new OrderCommentModel();
        return $order_model->orderCommentCount($map);
    }

    /**
     * 获取优惠券信息
     * @param $map
     * @param $field
     * @param $flag //true返回二维数组，false返回一维数组
     * @param $order
     * @param $page
     * @param $limit
     * @return array|false
     * */
    public function getCoupon($map,$field='*',$flag = true,$order='coupon_price desc',$page=1,$limit=10)
    {
        $couponModel = new CouponModel();
        return $couponModel->getCoupon($map,$field,$flag,$order,$page,$limit);
    }

    /**
     * 更新优惠券
     * @param $map
     * @param $data
     * @return int|false
     * */
    public function renewCoupon($map,$data)
    {
        $couponModel = new CouponModel();
        return $couponModel->updateCoupon($map,$data);
    }
}